package com.yanfan.accessControl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.yanfan.accessControl.domain.IbmsAccessControlDevice;
import com.yanfan.accessControl.domain.IbmsAccessControlRecords;
import com.yanfan.accessControl.service.IIbmsAccessControlDeviceService;
import com.yanfan.accessControl.service.IIbmsAccessControlRecordsService;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.ibms.common.utils.HttpUtil;
import com.yanfan.ibms.common.utils.HttpUtils2;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@RestController
public class ApiAccessControlController {
    @Autowired
    IIbmsAccessControlDeviceService iIbmsAccessControlDeviceService;

    @Autowired
    IIbmsAccessControlRecordsService ibmsAccessControlRecordsService;

    public void ApiImplicitParam (@RequestBody IbmsAccessControlDevice ibmsAccessControlDevice){
        // 发送POST请求
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("data","");

        JSONObject jsonObject = HttpUtils2
                .sendPost("http://10.21.10.248:8080/ocs/reformer/interface/getMjDeviceAll",
                        null, null);
        Object object = jsonObject.get("obj");
        List<IbmsAccessControlDevice> list = JSON.parseArray(object.toString(), IbmsAccessControlDevice.class);
        for (IbmsAccessControlDevice accessControlDevice : list) {
            accessControlDevice.setIsBusiness("住宅");
        }
        //批量更新或新增到数据
        iIbmsAccessControlDeviceService.saveOrUpdateBatch(list);
    }

    public void ApiIbmsAccessBusiness (){
        // 发送POST请求
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("data","");

        JSONObject jsonObject = HttpUtils2
                .sendPost("http://10.22.10.248:8080/ocs/reformer/interface/getMjDeviceAll",
                        null, null);
        Object object = jsonObject.get("obj");
        List<IbmsAccessControlDevice> list = JSON.parseArray(object.toString(), IbmsAccessControlDevice.class);
        for (IbmsAccessControlDevice accessControlDevice : list) {
            accessControlDevice.setIsBusiness("商业");
        }
        //批量更新或新增到数据
        iIbmsAccessControlDeviceService.saveOrUpdateBatch(list);
    }

    public void ApiIbmsAccessControlRecords(){
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        String nowTime = currentTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        // 计算一个小时之前的时间
        LocalDateTime oneHourAgo = currentTime.minusHours(1);
        String passTime = oneHourAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        JSONObject jso = new JSONObject();
        jso.put("curPage",1);
        jso.put("pageSize",100);
        jso.put("start",passTime);
        jso.put("end",nowTime);
//        jso.put("start","2024-10-01 00:11:11");
//        jso.put("end","2024-10-16 11:11:11");
        JSONObject jsonObject = HttpUtils2.sendPostBody(
                "http://10.21.10.248:8080/ocs/reformer/interface/MjOpenDoorRecord",
                null, jso);
        if(jsonObject.equals(null)){
            return;
        }
        JSONObject json = JSONObject.parseObject(jsonObject.get("obj").toString());
        Object object = json.get("list");
        List<IbmsAccessControlRecords> list = JSON.parseArray(object.toString(),
                IbmsAccessControlRecords.class);
        for (IbmsAccessControlRecords ibmsAccessControlRecords : list) {
                ibmsAccessControlRecords.setIsBusiness("住宅");
        }

        //批量更新或新增到数据库
        ibmsAccessControlRecordsService.saveOrUpdateBatch(list);
    }

    public void ApiIbmsAccessControlRecordsBusiness(){
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        String nowTime = currentTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        // 计算一个小时之前的时间
        LocalDateTime oneHourAgo = currentTime.minusHours(1);
        String passTime = oneHourAgo.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        JSONObject jso = new JSONObject();
        jso.put("curPage",1);
        jso.put("pageSize",100);
//        jso.put("start","2024-10-01 00:11:11");
//        jso.put("end","2024-10-16 11:11:11");
        jso.put("start",passTime);
        jso.put("end",nowTime);
        JSONObject jsonObject = HttpUtils2.sendPostBody(
                "http://10.22.10.248:8080/ocs/reformer/interface/MjOpenDoorRecord",
                null, jso);
        if(jsonObject.equals(null)){
            return;
        }
        JSONObject json = JSONObject.parseObject(jsonObject.get("obj").toString());
        Object object = json.get("list");
        List<IbmsAccessControlRecords> list = JSON.parseArray(object.toString(),
                IbmsAccessControlRecords.class);
        for (IbmsAccessControlRecords ibmsAccessControlRecords : list) {
            ibmsAccessControlRecords.setIsBusiness("商业");
        }
        //批量更新或新增到数据库
        ibmsAccessControlRecordsService.saveOrUpdateBatch(list);
    }
}
